package de.fhaugsburg.games.boardgames.scrabble.gui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import de.fhaugsburg.games.boardgames.Player.PlayerType;
import de.fhaugsburg.games.boardgames.exceptions.GameException;
import de.fhaugsburg.games.boardgames.scrabble.IScrabbleEngine;

public class NewgameDialog extends JFrame {

	
	private enum PlayerTypes {
		HUMAN ("Human player (local)"),
		CPU ("CPU player"),
		TERMINAL ("Human player (terminal)"),
		NOPLAYER ("-no player-");
		
		private String caption;

		private PlayerTypes(String s) {
			this.caption = s;
		}
		@Override
		public String toString(){
			return caption;
		}
	}
	
	//derive max players from engine
	private final int MAX_PLAYERS = IScrabbleEngine.MAX_PLAYERS;
	
	private Controller controller;
	private JComboBox[] playerType = new JComboBox[MAX_PLAYERS];
	private JTextField[] playerName = new JTextField[MAX_PLAYERS];
	
	
	
	public NewgameDialog(Controller controller) {
		this.controller = controller;
		buildGUI();
	}
	
	
	
	

	/**
	 * builds the window 
	 * @param locale
	 */
	private void buildGUI(){
		//clear all to rebuild, if necessary
//		this.removeAll();
		

		setTitle("Start a new game");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(200, 200, 480, 200);
		//center on screen
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		setLocation(screenSize.width/2 - getWidth()/2, screenSize.height/2 - getHeight()/2);
		
		setLayout(new BorderLayout());

		//master container
		JPanel settingsPanel = new JPanel();
		settingsPanel.setLayout(new BorderLayout()); 

		//where setting-boxes are
		JPanel playerPanel = new JPanel();
		playerPanel.setLayout(new GridLayout(0, 3));
		
		for (int i = 0; i < MAX_PLAYERS; i++) {
			playerPanel.add(new JLabel("Player " + (i+1) + ":"));
				playerType[i] = new JComboBox(PlayerTypes.values());
					playerType[i].setSelectedIndex(1);
			//		playerType1.addActionListener(this);
					playerPanel.add(playerType[i]);
				playerName[i] = new JTextField("Player " + (i+1));
					playerPanel.add(playerName[i]);
		}

		settingsPanel.add(BorderLayout.CENTER, playerPanel);
		settingsPanel.add(BorderLayout.SOUTH, new JSeparator());
		settingsPanel.setBorder(new EmptyBorder(15, 25, 5, 25));
		
		//the buttons
		JPanel buttonPanel = new JPanel();
		buttonPanel.setLayout(new BorderLayout());
		//OK, fire up new game
		JButton okButton = new JButton("Start new game");
			okButton.addActionListener(
				new ActionListener (){
					public void actionPerformed(ActionEvent e) {
						NewgameDialog.this.startNewgame();
					}
				}
			);
			buttonPanel.add(BorderLayout.CENTER, okButton);
		//Cancel closes the window, guessed? WHAT??!?
		JButton cancelButton = new JButton("Cancel");
			cancelButton.addActionListener(
				new ActionListener (){
					public void actionPerformed(ActionEvent e) {
						NewgameDialog.this.dispose();
					}
				}
			);

			buttonPanel.add(BorderLayout.WEST, cancelButton);

			
		//glue whole form together	
		add(BorderLayout.CENTER, settingsPanel);
		add(BorderLayout.SOUTH, buttonPanel);

		
		setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
		setResizable(false);
		pack();
		setVisible(true);
	}
	
	
	
	/**
	 * tells the engine to start a new game
	 */
	private void startNewgame(){
		//add all players to game
		for (int i = 0; i < MAX_PLAYERS; i++) {
			String playerNameString = playerName[i].getText();
				switch ((PlayerTypes) playerType[i].getSelectedItem()){
				case HUMAN:
						controller.addPlayer(playerNameString, PlayerType.LOCAL);
						break;
				case CPU:
						controller.addPlayer(playerNameString, PlayerType.CPU);
						break;
				case TERMINAL:
						controller.addPlayer(playerNameString, PlayerType.TERMINAL);
	//					   terminalServer.addNewTerminalSlot();
						break;
				case NOPLAYER:
					break;
				}
		}
		this.setVisible(false);
		controller.newGame();
		this.dispose();
	}

	
}
